import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { saveAs } from 'file-saver';

@Injectable()
export class LoginService {
  constructor(private http: HttpClient) {
  }

    /**
     * 加载地址
     */
    loadAddress() {
        return this.http.get('/system/api/unitinfo/loadaddress').pipe(
            map((res: Response) => res['data'])
        );
    }
  /** 获取登录界面信息 */
  getLoginPage() {
    return this.http.get('/assets/data/login/login.json').pipe(
      map((res: Response) => res['data'])
    );
  }

  /** 获取二维码 */
  getQrCode() {
    return this.http.get('/assets/data/login/qrcode.json').pipe(
      map((res: Response) => res['data'])
    );
  }

  /** 用户登录 */
  login(user: any) {
    return this.http.post('/system/api/login', user).pipe(
      map((res: Response) => res)
    );
  }

  /** 人名自动补全 */
  searchHumanName(value: string, code: string) {
    return this.http.post('/system/api/searchHumanName', null, {
      headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }),
      params: new HttpParams().set('value', value).set('code', code)
    }).pipe(
      map((res: Response) => res['data'])
    );
  }
  /**下载用户手册*/
  downloadTemplate(fileName, uploadType: number) {
    return this.http.get('/system/api/unitinfo/template?uploadType=' + uploadType, { 'responseType': 'blob' }).pipe(
      map(res => saveAs(res, fileName)));
  }
}
